#include "stdafx.h"

#include "Utils.h"
#include "control.h"

BOOL Utils::IsKeyProcessed(int nVirtKey)
{
	return (::GetKeyState(nVirtKey) & 0x8000) != 0;
}

static const wchar_t* strFmtCtlP = L"uie_control_p";

BOOL Utils::WriteClipboardPointerCtl(control* aCtl, HWND aHwnd)
{
	HGLOBAL		hglbCopy;
	control**	ppCtl;

	if (!OpenClipboard(aHwnd)) 
		return FALSE; 
	EmptyClipboard(); 

	hglbCopy = GlobalAlloc(GMEM_MOVEABLE, sizeof(control*)); 
	if (hglbCopy == NULL) 
	{ 
		CloseClipboard(); 
		return FALSE; 
	}

	ppCtl = (control**)GlobalLock(hglbCopy); 
	*ppCtl = aCtl;
	GlobalUnlock(hglbCopy);
	
	UINT uformat = ::RegisterClipboardFormat(strFmtCtlP);
	if (uformat != 0)
		SetClipboardData(uformat, hglbCopy); 
	
	CloseClipboard(); 

	return TRUE;
}

control* Utils::ReadClipboardPointerCtl(HWND aHwnd)
{
	control**	pRet;
	HGLOBAL		hglb;
	UINT uformat = ::RegisterClipboardFormat(strFmtCtlP);
	if (!IsClipboardFormatAvailable(uformat)) 
		return NULL; 
	if (!OpenClipboard(aHwnd)) 
		return NULL; 

	hglb = GetClipboardData(uformat); 
	if (hglb != NULL) 
	{
		pRet = (control**)GlobalLock(hglb); 
		if (pRet != NULL) 
		{ 
			GlobalUnlock(hglb);
			CloseClipboard(); 

			return *pRet;
		} 
	} 
	CloseClipboard();

	return NULL;
}